﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.IService;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Book
{
    public partial class Frm_StorageBook : BaseXtraForm
    {
        public Frm_StorageBook()
        {
            InitializeComponent();

            this.SetGridViewStyle(DetailView, false, false);
            this.BindRowIndicator(DetailView);

            #region TreeList属性
            this.SetTreeListStyle(treeList1);
            this.SetTreeListStyle(treeList2);
            #endregion

        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_StorageBook_Load(object sender, EventArgs e)
        {
            IBookService bservice = new BookService();

            treeList1.DataSource = bservice.GetAllMaterialCategory();
        }

        /// <summary>
        /// 物料分类焦点行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            IBookService bservice = new BookService();
            var focusCategory = treeList1.FocusedNode.GetValue("ID");

            //获取供应商
            var supplier = bservice.GetAllSupplierByCategoryID((Guid)focusCategory);
            treeList2.DataSource = supplier;

            if (supplier.Count == 0)
            {
                DetailGrid.DataSource = null;                
            }
        }

        /// <summary>
        /// 供应商焦点行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList2_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            IBookService bservice = new BookService();
            var focusCategory = treeList1.FocusedNode.GetValue("ID");
            var focusSupplier = treeList2.FocusedNode.GetValue("ID");

            DetailGrid.DataSource = bservice.GetDetailBySupplierID((Guid)focusCategory, (Guid)focusSupplier);
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Find_Click(object sender, EventArgs e)
        {
            IBookService bservice = new BookService();
            var focusCategory = treeList1.FocusedNode.GetValue("ID");
            var focusSupplier = treeList2.FocusedNode.GetValue("ID");

            var date1 = dateEdit1.DateTime;
            var date2 = dateEdit2.DateTime;

            if (focusSupplier != null)
            {
                DetailGrid.DataSource = bservice.GetDetailByFactor(date1,date2,(Guid)focusCategory,(Guid)focusSupplier);
            }
            else
            {
                DetailGrid.DataSource = null;
            }
        }

    }
}
